<?php

namespace app\queue\redis;

use app\admin\model\Bot;
use Illuminate\Support\Arr;
use support\Redis;
use Telegram\Bot\Api;
use Telegram\Bot\FileUpload\InputFile;
use Throwable;
use Webman\RedisQueue\Consumer;
use Illuminate\Support\Str;

class SetbeiFile implements Consumer
{
    // 要消费的队列名
    public $queue = 'setbei_file';

    private $beifen_name = "huoyunseo";
//    private $beifen_token = "7205363708:AAG-_CH37LDK_XEaSbOxeRR4kcj6zpUwB3k";
    private $beifen_token = "7264181945:AAEkf5eJ06QREetU8y0kTccliRAxWUv8WFg";
    private $beifen_id = "5868952909";

    // 消费
    public function consume($data)
    {
        file_put_contents(base_path('a.txt'), json_encode($data), FILE_APPEND);
        $type = Arr::get($data, 'type', '');
        $file_id = Arr::get($data, 'file_id', '');

        if ($type && $file_id){
            $token = $this->beifen_token;
            $telegram = new Api($token);

            $method  = 'send'. ucfirst(Str::camel($type));
            dump("队列数据");
            $file = $telegram->getFile([
                'file_id'   =>  $file_id,
            ]);
            dump($file);
            $response = $telegram->{$method}([
                'chat_id' => $this->beifen_id,
                $type => InputFile::create("https://api.telegram.org/file/bot{$token}/{$file->file_path}"),
                'caption' => '备份文件'
            ]);
            dump("队列数据");
            dump($response);
        }

    }



}
